package cuiyt.leetcode.easy;

/**
 * @auth cuiyutian1
 * @date 2021/8/10 17:42
 * @description:
 */
public class T14 {

    public static String longestCommonPrefix(String[] strs) {
        if(strs == null || strs.length == 0){
            return "";
        }
        String res = strs[0];
        for (int i = 1; i < strs.length; i++) {
            res = commonPrefixByTwoString(res,strs[i]);
        }
        return res;
    }

    public static String commonPrefixByTwoString(String str1, String str2) {
        int minLen = Integer.min(str1.length(),str2.length());
        int preFixStart = 0;
        for(int i = 0; i < minLen; i++){
            if(preFixStart == i && str1.charAt(i) == str2.charAt(i)){
                preFixStart++;
            }
        }
        return str1.substring(0,preFixStart);
    }

    public static void main(String[] args) {
        String[] input = {"aaa","aa","aaa"};
        System.out.println(longestCommonPrefix(input));
    }
}
